package cn.hanyx.coder.design.pattern.creational.singleton;

import java.util.concurrent.atomic.AtomicLong;

/**
 * IdGenerator
 *
 * @author hanyx
 * @date 2020/05/12
 */
public class IdGenerator {

    private static final IdGenerator instance = new IdGenerator();

    private IdGenerator() {
    }

    private AtomicLong id = new AtomicLong(0);

    private Long getId() {
        return id.incrementAndGet();
    }

    public static IdGenerator getInstance() {
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> System.out.println(IdGenerator.getInstance().getId())).start();
        }
    }
}
